package com.openatc.agent.service;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import com.openatc.comm.data.MessageData;
import com.openatc.core.util.GsonUtil;
import com.openatc.model.model.ControlPattern;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

import java.util.logging.Logger;


/**
 * @author ：panleilei
 * @description
 * @modified By：
 * @version:
 * @date ：Created in 2024/10/11 14:29
 */
@Component
public class PermissionValidator {
    private Gson gson = GsonUtil.getInstance();
    private static final Logger logger = Logger.getLogger(PermissionValidator.class.getSimpleName());
    // 配置工具-手动控制
    public static final String CONFIGER_MANUAL_RENEW = "configer:manual:renew"; // 恢复
    public static final String CONFIGER_MANUAL_BASE = "configer:manual:base"; // 基础控制
    public static final String CONFIGER_MANUAL_SENIOR = "configer:manual:senior"; // 特殊控制
    public static final String CONFIGER_MANUAL_DWELL = "configer:manual:dwell"; // 步进
    public static final String CONFIGER_MANUAL_OPTIMIZE = "configer:manual:optimize"; // 优化
    public static final String CONFIGER_MANUAL_PREEMPT = "configer:manual:preempt"; // 优先
    // 配置工具-渠化
    public static final String CONFIGER_CANALIZATION = "configer:canalization";
    // 配置工具-特征参数下载
    public static final String CONFIGER_FEATURE_DOWNLOAD = "configer:download";
    // 配置工具-系统
    public static final String CONFIGER_SYSTEM_INFO = "configer:system:info"; // 信息
    public static final String CONFIGER_SYSTEM_TIME = "configer:system:time"; // 对时设置
    public static final String CONFIGER_SYSTEM_REMOTE = "configer:system:remote"; // 远程控制
    public static final String CONFIGER_SYSTEM_CHANNELTEST = "configer:system:channeltest"; // 通道检测
    public static final String CONFIGER_SYSTEM_DEVICEINFO = "configer:system:deviceinfo"; // 设备信息
    public static final String CONFIGER_SYSTEM_DOWNLOAD = "configer:system:download"; // 设备信息下载

    /**
     * 用户需要有configer:download权限才可以下载特征参数
     * @param subject
     * @return
     */
    public boolean validateFeatureDownload(Subject subject) {
        return subject.isPermitted(CONFIGER_FEATURE_DOWNLOAD);
    }

    /**
     * 校验用户的控制权限
     * @param subject
     * @param requestData
     * @return
     */
    public boolean validateControl(Subject subject, MessageData requestData) {
        JsonElement data = requestData.getData();
        if (data == null) return false;
        ControlPattern controlPattern;
        try {
            controlPattern = gson.fromJson(data, ControlPattern.class);
        } catch (JsonSyntaxException e) {
            logger.warning("Valid user control permission failed! Request data: " + requestData);
            return false;
        }
        int control = controlPattern.getControl();
        switch (control) {
            case 0: // 恢复
                return subject.isPermitted(CONFIGER_MANUAL_RENEW);
            case 1:
            case 2:
            case 3: // 特殊控制
                return subject.isPermitted(CONFIGER_MANUAL_SENIOR);
            case 4: // 步进控制
                return subject.isPermitted(CONFIGER_MANUAL_DWELL);
            case 23: // 相位关断
            case 100: // 临时方案
            case 22: // 相位锁定
                return subject.isPermitted(CONFIGER_MANUAL_OPTIMIZE);
            case 24: // 优先控制
                return subject.isPermitted(CONFIGER_MANUAL_PREEMPT);
            default: // 基础控制
                return subject.isPermitted(CONFIGER_MANUAL_BASE);
        }
    }

    /**
     * 检查用户的系统权限
     * @param subject
     * @param infotype
     * @return
     */
    public boolean validateSystemControl(Subject subject, String infotype) {
        switch (infotype) {
            case "system/custom":
                return subject.isPermitted(CONFIGER_SYSTEM_INFO);
            case "system/time":
                return subject.isPermitted(CONFIGER_SYSTEM_TIME);
            case "system/remote":
                return subject.isPermitted(CONFIGER_SYSTEM_REMOTE);
            case "system/channelcheck":
                return subject.isPermitted(CONFIGER_SYSTEM_CHANNELTEST);
            case "system/deviceinfo":
                return subject.isPermitted(CONFIGER_SYSTEM_DEVICEINFO);
            case "system/faultclear":
            case "system/update":
            case "system/centerip":
            case "system/ip1":
            case "system/ip2":
            case "system/serialport":
            case "system/optstatparam":
            case "system/udiskupdate":
                return subject.isPermitted(CONFIGER_SYSTEM_DOWNLOAD);
        }
        return false;
    }
}
